package com.chj.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Enumeration;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UrlPathHelper;

@Component
public class UrlPathUtil {

	private static UrlPathUtil urlPathUtil;
	@Autowired
	private UrlPathHelper urlPathHelper;

	@PostConstruct
	public void init() {
		urlPathUtil = this;
	}

	public static void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
		urlPathUtil.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
	}

	public static void setUrlDecode(boolean urlDecode) {
		urlPathUtil.urlPathHelper.setUrlDecode(urlDecode);
	}

	public static void setRemoveSemicolonContent(boolean removeSemicolonContent) {
		urlPathUtil.urlPathHelper.setRemoveSemicolonContent(removeSemicolonContent);
	}

	public static boolean shouldRemoveSemicolonContent() {
		return urlPathUtil.urlPathHelper.shouldRemoveSemicolonContent();
	}

	public static void setDefaultEncoding(String defaultEncoding) {
		urlPathUtil.urlPathHelper.setDefaultEncoding(defaultEncoding);
	}

	public static String getLookupPathForRequest(HttpServletRequest request) {
		return urlPathUtil.urlPathHelper.getLookupPathForRequest(request);
	}

	public static String getPathWithinServletMapping(HttpServletRequest request) {
		return urlPathUtil.urlPathHelper.getPathWithinServletMapping(request);
	}

	public static String getPathWithinApplication(HttpServletRequest request) {
		return urlPathUtil.urlPathHelper.getPathWithinApplication(request);
	}

	public static String getRequestUri(HttpServletRequest request) {
		return urlPathUtil.urlPathHelper.getRequestUri(request);
	}

	public static String getContextPath(HttpServletRequest request) {
		return urlPathUtil.urlPathHelper.getContextPath(request);
	}

	public static String getServletPath(HttpServletRequest request) {
		return urlPathUtil.urlPathHelper.getServletPath(request);
	}

	public static String getHeaderVal(HttpServletRequest request, String headerKey) {
		String value = request.getHeader(headerKey);

		Enumeration<String> headerNames = request.getHeaderNames();
		while (headerNames.hasMoreElements()) {
			String key = headerNames.nextElement();
			String keyvalue = request.getHeader(key);
			System.out.println("HeaderParam:Key:" + key + ";keyvalue:" + keyvalue);
		}

		return value;
	}

	public static <T> T getResBody(HttpServletRequest request, Class<T> clazz) {
		try {
			BufferedReader br = request.getReader();
			String inputLine;
			StringBuilder str = new StringBuilder();
			while ((inputLine = br.readLine()) != null) {
				str.append(inputLine);
			}
			br.close();
			if (StringUtils.isEmpty(str.toString())) {
				return null;
			}
			System.out.println(str.toString());
			return JsonUtil.getEntity(str.toString(), clazz);
		} catch (IOException e) {
			return null;
		}
	}

}